#ifndef _PLAYER_HPP_20120626
#define _PLAYER_HPP_20120626

#include "base.hpp"
#include "connection.hpp"

#include "cmd_comm.hpp"
#include "cmd_type.hpp"

#include "table.hpp"
#include "area.hpp"

using namespace cmd;
using namespace mina;

class Player
{
	public :
		Player(Connection & conn, const RoleB & role_b, const Position & position);
		~Player();

		RID id() const { return _role_b._id; }

		template <typename _Msg>
			int handle(Connection & conn, _Msg & msg);

		void table(Table * tbl) { _table = tbl; }

	public :
		Connection *  _conn;
		RoleB         _role_b;
		Table *       _table;

		DECL_LOGGER(logger);
};

template <typename _Msg>
int Player::handle(Connection & conn, _Msg & msg)
{
	const Position & pos = _role_b._position;
	Table * table = Area::instance().find_table(pos._room_id, pos._table_id);

	if (NULL == table)
	{
		LOG4CPLUS_ERROR(logger, "not found table. rid: " << id()
								<< ", romm_id: " << (int)pos._room_id
								<< ", table_id: " << (int)pos._table_id
								<< ", cmd_0: " << (int)msg._head._cmd_0
								<< ", cmd_1: " << (int)msg._head._cmd_1);
		return 0;
	}

	return table->handle(this, conn, (void*)&msg);
}

#endif

